package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"zg3/internal/api/request"
	"zg3/internal/api/response"
	"zg3/internal/models/service"
)

func UserRegister(c *gin.Context) {
	var json request.UserRegister
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	register, s := service.UserRegister(&json)
	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	response.Response(c, 200, "注册成功", register)

}

func UserLogin(c *gin.Context) {
	var json request.UserLogin
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	login, s := service.UserLogin(&json)
	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	response.Response(c, 200, "登录成功", login)

}

func UserUpdate(c *gin.Context) {
	var json request.UserUpdate
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	update := service.UserUpdate(&json, userId)

	if update != "" {
		response.Response(c, 400, update, nil)
		return
	}

	response.Response(c, 200, "修改成功", nil)

}
